package org.dreamwork.plugins.network.assistant.gui

/**
 * Created by seth.yang on 2018/5/7
 */
object Utils {
    @JvmStatic
    fun toHex(input: String): ByteArray {
        var text = input
        text = text.replace("\\s+".toRegex(), "")
        if (text.length % 2 != 0) {
            text = "0$text"
        }
        val chs = text.toLowerCase().toCharArray()
        val buff = ByteArray(chs.size / 2)
        var pos = 0
        var n: Int
        var ch: Int
        var i = 0
        while (i < chs.size) {
            ch = chs[i].toInt()
            n = if (ch >= '0'.toInt() && ch <= '9'.toInt()) {
                ch - '0'.toInt()
            } else if (ch >= 'a'.toInt() && ch <= 'f'.toInt()) {
                ch - 'a'.toInt() + 10
            } else {
                throw IllegalArgumentException("Invalid data")
            }
            n = n shl 4
            ch = chs[i + 1].toInt()
            if (ch >= '0'.toInt() && ch <= '9'.toInt()) {
                n = n or ch - '0'.toInt()
            } else if (ch >= 'a'.toInt() && ch <= 'f'.toInt()) {
                n = n or ch - 'a'.toInt() + 10
            }
            buff[pos++] = n.toByte()
            i += 2
        }
        return buff
    }
}

class Config {
    lateinit var head: ByteArray
    lateinit var tail: ByteArray
    var type: CRCType? = null
    var includeHead = false
}
enum class CRCType(val code: Int, private val chinese: String) {
    Sum8(0, "算术和"), NSum8(1, "算术负和"), XOR8(2, "异或"), CRC16(3, "CRC16");

    override fun toString(): String {
        return chinese
    }
}